﻿using System;
using System.Collections.Generic;
using System.Web;
using System.Data;
using System.Collections;
using He.Common;
using He.Web.UI;

namespace He.Web.tools
{
    /// <summary>
    /// watermark_ajax 的摘要说明
    /// </summary>
    public class watermark_ajax : He.Web.UI.AjaxHandler
    {
        He.Model.webset webset = new He.BLL.webset().GetWebset();
        public override void ProcessRequest(HttpContext context)
        {
            //是否登录
            UI.Manage bk = new UI.Manage();
            if (!bk.IsAdminLogin())
            {
                EchoJson(0, "您还未登陆，请先登陆");
                return;
            }
            //是否开启水印
            if (webset.watermarktype <= 0)
            {
                EchoJson(0, "您还未开启图片水印功能，请进入系统图片设置开启！");
                return;
            }

            string act = InputUtil.Get("act");

            switch (act)
            {
                case "all": //全部加水印
                    MarkAll();
                    
                    break;
            }


        }

        #region 全部打水印===================================
        private void MarkAll()
        {
            int cid;
            if (!int.TryParse(InputUtil.Get("cid"), out cid))
            {
                return;
            }
            He.BLL.article bll = new BLL.article();
            DataTable dt = bll.GetList(cid, "", "Id desc").Tables[0];
            foreach (DataRow dr in dt.Rows)
            {
                if (dr["ImgUrl"].ToString().Trim() != "" && dr["img_urls"].ToString().Trim() != "")
                {
                    string[] imgArr = dr["img_urls"].ToString().Split(',');
                    foreach (string imgurl in imgArr)
                    {
                        string fileExtension = System.IO.Path.GetExtension(imgurl);//'获取扩展名
                        //是否打图片水印
                        if (IsWaterMark(fileExtension.Trim('.')) && webset.watermarktype > 0)
                        {
                            switch (this.webset.watermarktype)
                            {
                                case 1:
                                    WaterMark.AddImageSignText(imgurl, imgurl,
                                        this.webset.watermarktext, this.webset.watermarkposition,
                                        this.webset.watermarkimgquality, this.webset.watermarkfont, this.webset.watermarkfontsize);
                                    break;
                                case 2:
                                    WaterMark.AddImageSignPic(imgurl, imgurl,
                                        this.webset.watermarkpic, this.webset.watermarkposition,
                                        this.webset.watermarkimgquality, this.webset.watermarktransparency);
                                    break;
                            }
                        }
                    }
                }
            }
            EchoJson(1, "水印添加成功！");
        }
        #endregion

        /// <summary>
        /// 是否需要打水印
        /// </summary>
        /// <param name="_fileExt">文件扩展名，不含“.”</param>
        private bool IsWaterMark(string _fileExt)
        {
            //判断是否开启水印
            if (this.webset.watermarktype > 0)
            {
                //判断是否可以打水印的图片类型
                ArrayList al = new ArrayList();
                al.Add("bmp");
                al.Add("jpeg");
                al.Add("jpg");
                al.Add("png");
                if (al.Contains(_fileExt.ToLower()))
                {
                    return true;
                }
            }
            return false;
        }
        public override bool IsReusable
        {
            get
            {
                return false;
            }
        }
    }
}